package com.mproject.activity.project;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.mproject.activity.R;
import com.mproject.activity.TabGroupActivity;
import com.mproject.adapter.Member_List_Adapter;
import com.mproject.customclass.Project;
import com.mproject.customclass.Student;
import com.mproject.network.IVLE_API_Helper;
import com.mproject.network.ProgressBarManager;

public class Project_Info extends Activity implements OnClickListener{

	public static SharedPreferences mypref;
	private ListView member_listview;

	private Project proj;

	private TextView add_member;
	private TextView project_summary;

	//List of combined members
	private List<Student> members;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.project_info); 

		add_member = (TextView)findViewById(R.id.project_info_add);
		add_member.setOnClickListener(this);
		project_summary = (TextView)findViewById(R.id.project_info_sum);
		project_summary.setOnClickListener(this);

		/*Bundle extras=getIntent().getExtras();
		Project proj =(Project)extras.getSerializable("project");
		Log.e("PROJECT", proj.getID());
		Log.e("PROJECT", proj.getDescription());
		Log.e("PROJECT", proj.getCategory());
		Log.e("PROJECT", proj.getTitle());*/

	}

	public void onResume() {
		super.onResume();
		project_summary.setVisibility(View.INVISIBLE);
		loadingContent();


	}

	public void onPause() {
		super.onPause();
		member_listview.setAdapter(null);
		add_member.setVisibility(View.INVISIBLE);
	}

	/*public void onPause() {
		super.onPause();
		members.clear();
		member_listview.invalidateViews();
	}*/

	public void loadingContent() {
		ProgressBarManager manager=new ProgressBarManager(Project_Info.this.getParent())
		{

			protected void onPreExecute() {

				Log.v("manager"," in pre execute");

				dialog.setMessage("Loading please wait...");
				dialog.setIndeterminate(true);

				// dialog.setTitle("Manager");
				dialog.show();
			}

			protected Boolean doInBackground(String... params) 
			{

				try {
					members = new ArrayList<Student>();
					//fill project list of the user
					mypref=getSharedPreferences("My_Prefs",Activity.MODE_WORLD_WRITEABLE);
					proj = new Project(mypref.getString("project_id", ""));
					proj.setTitle(mypref.getString("project_title", ""));
					proj.setDescription(mypref.getString("project_des", ""));
					proj.setCategory(mypref.getString("project_cat", ""));
					proj.setManager(mypref.getBoolean("project_is_manager", true));
					proj.setOpenDate(mypref.getString("project_open", ""));
					proj.setExpiryDate(mypref.getString("project_expiry", ""));
					IVLE_API_Helper.setProjectMembers(mypref.getString("IVLE_Token", ""), proj,mypref.getString("user_id", ""));

					for (Student mem : proj.getManagers()) {
						members.add(mem);
					}
					for (Student mem : proj.getMembers()) {
						members.add(mem);
					}
					return true;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return false;
				}


			}
			protected void onPostExecute(Boolean result) {
				this.getDialog().dismiss();
				if (result == false) {
					AlertDialog.Builder builder = new AlertDialog.Builder(Project_Info.this.getParent());
					builder.setMessage("No internet connection")
					.setNegativeButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}

					});
					AlertDialog alert = builder.create();
					alert.show();
				}
				else {
					project_summary.setVisibility(View.VISIBLE);
					fillTheList();
					if (proj.isManager()) {
						add_member.setVisibility(View.VISIBLE);
					}
					else
						add_member.setVisibility(View.INVISIBLE);
				}
			}
		};
		manager.execute();
	}

	public void fillTheList() {
		member_listview = (ListView)findViewById(R.id.member_listview);
		Member_List_Adapter adapter = new Member_List_Adapter(Project_Info.this,getApplicationContext(), R.layout.member_list_row,members);
		member_listview.setAdapter(adapter);

		member_listview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> listView, View itemView, int position, long id) {
				Intent intent = new Intent(Project_Info.this.getParent(), Project_Info_Member_Detail.class);
				intent.putExtra("project", proj);
				intent.putExtra("member", members.get(position));
				TabGroupActivity parentActivity = (TabGroupActivity)Project_Info.this.getParent();
				parentActivity.startChildActivity("Project_Info_Member_Detail", intent);

			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == add_member) {
			Intent intent = new Intent(Project_Info.this.getParent(), Project_Info_Add_Member.class);
			intent.putExtra("project", proj);
			TabGroupActivity parentActivity = (TabGroupActivity)Project_Info.this.getParent();
			parentActivity.startChildActivity("Project_Info_Add_Member", intent);
		}
		if (v == project_summary) {
			Intent intent = new Intent(Project_Info.this.getParent(), Project_Info_Summary.class);
			intent.putExtra("project", proj);
			TabGroupActivity parentActivity = (TabGroupActivity)Project_Info.this.getParent();
			parentActivity.startChildActivity("Project_Info_Summary", intent);
		}

	}
}